package o2o.web.actions.sys;

import com.jinyou.utils.common.ValidateUtil;
import o2o.cache.agent.UserAgentRelationMemory;
import o2o.cache.user.UserInfoMemory;
import o2o.dao.PO.common.UserType;
import o2o.dao.PO.sys.SysCityAdmin;
import o2o.dao.PO.user.UserInfo;
import o2o.web.actions.company.SysCityCompanyMan;
import o2o.system.data.SYS_SETTINGS;

import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * 抢单模式
 */
public class GrabOrderModeMan {


    // 根据商圈获取 当前的订单模式 0 混合模式 1 仅抢单模式 2 仅调度模式 3 调度优先模式
    public static int getGrabOrderMode(Long agentId) {
        Set<String> adminSet = UserAgentRelationMemory.getInstance().usernameSetOfAgentId(agentId);
        adminSet = adminSet == null ? Collections.EMPTY_SET : adminSet;
        UserInfo user = null;
        if ((!adminSet.isEmpty()) && ((user = UserInfoMemory.getInstance().getUserInfo(adminSet.iterator().next())) != null) // 商圈的代理用户不为空
        ) { // 上下级代理模式
            return getGrabOrderModeByAdmin(user.getUsername());
        } else {
            if (null == SYS_SETTINGS.GRAB_ORDER_MODE) {
                return 0;
            } else {
                return SYS_SETTINGS.GRAB_ORDER_MODE;
            }
        }
    }

    // 通过代理的账号 获取当前的订单模式是什么样子的
    public static int getGrabOrderModeByAdmin(String adminUsername) {
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(adminUsername);
        if (null == SYS_SETTINGS.GRAB_ORDER_MODE) {
            return 0;
        } else {
            return SYS_SETTINGS.GRAB_ORDER_MODE;
        }
    }

    // 根据订单查看当前代理是开启了调度
    public static int getDispatchOrderModeByAgentId(Long agentId) {
        if (SYS_SETTINGS.IS_OPEN_AUTO_DISPATCH_ORDER != null && SYS_SETTINGS.IS_OPEN_AUTO_DISPATCH_ORDER > 0) {
            if (SYS_SETTINGS.ADMIN_MANAGER_DISPATCH_ORDER_MODE > 0) {
                Set<String> adminSet = UserAgentRelationMemory.getInstance().usernameSetOfAgentId(agentId);
                adminSet = adminSet == null ? Collections.EMPTY_SET : adminSet;
                UserInfo user = null;
                if ((!adminSet.isEmpty()) && ((user = UserInfoMemory.getInstance().getUserInfo(adminSet.iterator().next())) != null)) {
                    return getDispatchOrderModeByAdmin(user.getUsername());
                }
            }
            return SYS_SETTINGS.IS_OPEN_AUTO_DISPATCH_ORDER;
        } else {
            return 0;
        }
    }

    // 通过代理的账号 获取当前代理是否需要进行自动派单
    public static int getDispatchOrderModeByAdmin(String adminUsername) {

        return SYS_SETTINGS.IS_OPEN_AUTO_DISPATCH_ORDER == null ? 0 : SYS_SETTINGS.IS_OPEN_AUTO_DISPATCH_ORDER;
    }
}
